package com.example.by.test;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

/**
 * Created by BY on 2016/4/25.
 */
public class Window {
    private Context context;
    private boolean isAdded = false; // 是否已增加悬浮窗
    private static WindowManager wm;
    private static WindowManager.LayoutParams params;
    private Button btn_floatView;

    public Window(Context context) {
        this.context = context;
        createFloatView();
    }

    private void createFloatView() {
        btn_floatView = new Button(context);
        btn_floatView.setText("悬浮窗");
        btn_floatView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("click");            }
        });

        wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        params = new WindowManager.LayoutParams();

        // 设置window type
        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
       /*
        * 如果设置为params.type = WindowManager.LayoutParams.TYPE_PHONE;
        * 那么优先级会降低一些, 即拉下通知栏不可见
        */

        params.format = PixelFormat.RGBA_8888; // 设置图片格式，效果为背景透明

        // 设置Window flag
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
       /*
        * 下面的flags属性的效果形同“锁定”。
        * 悬浮窗不可触摸，不接受任何事件,同时不影响后面的事件响应。
       wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL
                              | LayoutParams.FLAG_NOT_FOCUSABLE
                              | LayoutParams.FLAG_NOT_TOUCHABLE;
        */

        // 设置悬浮窗的长得宽
        params.width = 200;
        params.height = 200;

        // 设置悬浮窗的Touch监听
        btn_floatView.setOnTouchListener(new View.OnTouchListener() {
            int lastX, lastY;
            int paramX, paramY;

            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        paramX = params.x;
                        paramY = params.y;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int dx = (int) event.getRawX() - lastX;
                        int dy = (int) event.getRawY() - lastY;
                        params.x = paramX + dx;
                        params.y = paramY + dy;
                        // 更新悬浮窗位置
                        wm.updateViewLayout(btn_floatView, params);
                        break;
                }
                return false;
            }
        });

        wm.addView(btn_floatView, params);
        isAdded = true;
    }
}
